@echo off
setlocal
set _SCRIPTDIR=%~dp0foobar\
set _SCRIPTDIR=%_SCRIPTDIR:\foobar\=%

set _MAINGPGHOME=%APPDATA%\gnupg

rem Find the requested command.
for /F %%v in ('echo %1^|findstr "^genkeys$ ^gpgexport$ ^gpgimport$ ^gpg"') do call :exec set COMMAND=%%v

if "%COMMAND%" == "" (
    echo Usage: %0 { genkeys : dogpg }
    pause
    goto :eof
) else (
    shift
)

call :%COMMAND% %1 %2 %3 %4 %5 %6 %7 %8 %9
rem if errorlevel 1 pause
goto :eof

:gpgsetup
set _KSHOME=%_SCRIPTDIR%
set GNUPGHOME=%_KSHOME%
goto :eof

:: %1 keyqualifier RIPEMD160
:gpggenkey
rem rd /q /s "%_KSHOME%"
rem mkdir "%_KSHOME%"
del /q "%_KSHOME%\*.gpg"
del /q "%_KSHOME%\*.bak"
gpg --no-tty --s2k-cipher-algo AES --s2k-digest-algo SHA1 --s2k-mode 3 --gen-key --batch "%_SCRIPTDIR%\genkey-%1.txt"
rem gpg -a --export-secret-key > "%_SCRIPTDIR%\sec-key-%1.asc"
rem gpg -a --export > "%_SCRIPTDIR%\pub-key-%1.asc"
gpg -a --export-secret-key > "%_SCRIPTDIR%\..\..\..\src\xpi\chrome\content\test\sec-key-%1.asc"
gpg -a --export > "%_SCRIPTDIR%\..\..\..\src\xpi\chrome\content\pub-key-%1.asc"
goto :eof

:gpgexport
call :gpgsetup
gpg -a --export-secret-key > "%_SCRIPTDIR%\sec-keys.asc"
gpg -a --export > "%_SCRIPTDIR%\pub-keys.asc"
goto :eof

:gpgimport
del /q "%_MAINGPGHOME%\*.gpg"
gpg -vvv --import "%_SCRIPTDIR%\sec-keys.asc"
goto :eof

:: %1 user
:: %2 targetuser
:: %3 passphrase
:gpgencryptsign
del "%_SCRIPTDIR%\text1-%12%2-es.asc"
echo/%3|gpg -u "%1" -r "%2" --armour --passphrase-fd 0 %4 %5 %6 %7 %8 %9 ^
 --trust-model always --output "%_SCRIPTDIR%\text1-%12%2-es.asc" ^
 --sign --encrypt "%_SCRIPTDIR%\..\loremipsum.txt"
goto :eof

:: %1 user
:: %2 targetuser
:: %3 passphrase
:gpgencrypt
del "%_SCRIPTDIR%\text1-%12%2-eo.asc"
echo/%3|gpg -r "%2" --armour --passphrase-fd 0 %4 %5 %6 %7 %8 %9 ^
 --trust-model always --output "%_SCRIPTDIR%\text1-%12%2-eo.asc" ^
 --encrypt "%_SCRIPTDIR%\..\loremipsum.txt"
goto :eof

:: %1 user
:: %2 targetuser
:: %3 passphrase
:gpgencryptanon
del "%_SCRIPTDIR%\text1-%12%2-ea.asc"
echo/%3|gpg -R "%2" --armour --passphrase-fd 0 %4 %5 %6 %7 %8 %9 ^
 --trust-model always --output "%_SCRIPTDIR%\text1-%12%2-ea.asc" ^
 --encrypt "%_SCRIPTDIR%\..\loremipsum.txt"
goto :eof

:: %1 user
:: %2 passphrase   --force-v3-sigs --digest-algo RIPEMD160 
:gpgsign
del "%_SCRIPTDIR%\text1-%1-so.asc"
echo/%2|gpg -u "%1" --armour --passphrase-fd 0 %3 %4 %5 %6 %7 %8 %9 ^
 --trust-model always --output "%_SCRIPTDIR%\text1-%1-so.asc" ^
 --sign "%_SCRIPTDIR%\..\loremipsum.txt"
goto :eof

:genkeys
call :gpgsetup
call :gpggenkey lucidity
rem call :gpggenkey patrick
rem call :gpggenkey woody
rem call :gpggenkey darla
del /q "%_SCRIPTDIR%\*.gpg"
del /q "%_SCRIPTDIR%\*.bak"
del /q "%_MAINGPGHOME%\*.gpg"
set _KSHOME=%_MAINGPGHOME%
set GNUPGHOME=%_KSHOME%
rem gpg -vvv --import "%_SCRIPTDIR%\sec-key-patrick.asc"
rem gpg -vvv --import "%_SCRIPTDIR%\sec-key-woody.asc"
rem gpg -vvv --import "%_SCRIPTDIR%\pub-key-darla.asc"
gpg --list-keys -v
goto :eof

:gpg
call :gpgsetup
rem call :gpgencryptsign woody@emergent.org patrick@woodworth.org %1 %2 %3 %4 %5 %6 %7 %8 %9
rem call :gpgsign woody@emergent.org %1 %2 %3 %4 %5 %6 %7 %8 %9
gpg %1 %2 %3 %4 %5 %6 %7 %8 %9
rem call :gpgencryptsign woody@emergent.org patrick@woodworth.org asdf %1 %2 %3 %4 %5 %6 %7 %8 %9
rem call :gpgencrypt woody@emergent.org patrick@woodworth.org asdf %1 %2 %3 %4 %5 %6 %7 %8 %9
rem call :gpgsign woody@emergent.org asdf %1 %2 %3 %4 %5 %6 %7 %8 %9
goto :eof

:exec
%*
goto :eof

